package tk.kolesnikov.tests;

import tk.kolesnikov.algorithms.DCTHelper;
import tk.kolesnikov.algorithms.ImageUtils;
import tk.kolesnikov.algorithms.MatrixHelper;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * Created by seko0313 on 17.01.14.
 */
public class DCTTest {

    public static void main(String[] args) throws Exception {
        //test1();
        test2();
    }

    private static void test2() throws Exception {

        System.out.println("start");

        ImageUtils imageUtils = new ImageUtils();
        BufferedImage image = ImageIO.read(new File("images" + File.separator + "test.png"));
        image = imageUtils.resize(image, 128, 128);
        image = imageUtils.graysсale(image);

        System.out.println("Image prepared");

        double[][] imageMatrix = imageUtils.grayscaleImageToMatrix(image);
        double[][] dctTransformed;

        //dctTransformed = imageMatrix;
        dctTransformed = DCTHelper.dct2(imageMatrix, 0);
        //dctTransformed = DCTHelper.idct2(dctTransformed, 0);

        System.out.println("dct ready");

        int rgb, color;
        for(int x =0; x < image.getWidth(); x++)
        for(int y = 0; y < image.getHeight(); y++) {
            color = (int )dctTransformed[x][y];
            rgb = imageUtils.getIntRgbFromColors(color, color, color);
            image.setRGB(x, y, rgb);
        }

        System.out.println("new colors set");

        File outputfile = new File("dct_test.png");
        ImageIO.write(image, "png", outputfile);

        System.out.println("finish");
    }

    private static void test1() throws Exception {
        double[][] matrix = {
                {1,2,3},
                {4,5,6},
                {7,8,9}
        };

        double[][] dctMatrix;

        int offset = 0;

        dctMatrix = DCTHelper.dct2in2dimension(matrix, offset);
        MatrixHelper.printMatrix(dctMatrix);

        dctMatrix = DCTHelper.idct2in2dimension(dctMatrix, offset);
        MatrixHelper.printMatrix(dctMatrix);

        dctMatrix = DCTHelper.dct2(matrix, offset);
        MatrixHelper.printMatrix(dctMatrix);

        dctMatrix = DCTHelper.idct2(dctMatrix, offset);
        MatrixHelper.printMatrix(dctMatrix);
    }

}
